package com.ai.wxy.spring.storm.test.drpc;

import lombok.extern.slf4j.Slf4j;
import org.apache.storm.coordination.BatchOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBatchBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * 统计粉丝量,url
 *
 * @author 石头
 * @Date 2019/12/18
 * @Version 1.0
 **/
@Slf4j
public class CountAggregatorBatchBlot extends BaseBatchBolt<Object> {
    private Object requestId;
    private BatchOutputCollector collector;
    private int reachCount;
    @Override
    public void prepare(Map<String, Object> conf, TopologyContext context, BatchOutputCollector collector, Object id) {
        this.requestId = id;
        this.collector = collector;
    }

    @Override
    public void execute(Tuple tuple) {
        reachCount += tuple.getInteger(1);
    }

    @Override
    public void finishBatch() {
        collector.emit(new Values(this.requestId, this.reachCount));
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("Id", "ReachCount"));
    }
}
